package com.devshephard.rpcframework.acm.warmup.demo;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public class WarmUpSimpleDemo {

    //阈值QPS
    public static volatile AtomicLong MAX_QPS = new AtomicLong(100);
    //冷却时间
    public static volatile AtomicLong WARMUP_SEC = new AtomicLong(5);
    //桶最大令牌数
    public static volatile AtomicLong BUCKET_CAPCITY = new AtomicLong(100);
    //每次令牌掉落数（初始值为0）
    public static volatile AtomicLong TOKEN_NUM = new AtomicLong(0);
    //令牌掉落速率(秒)
    public static volatile AtomicLong TOKEN_DROP_SPEED = new AtomicLong(0);

    public static void main(String[] args) throws InterruptedException {

        //计算令牌掉落速率（斜率）
        TOKEN_DROP_SPEED.compareAndSet(0,MAX_QPS.get() / WARMUP_SEC.get());

        for (int i = 0; i < WARMUP_SEC.get(); i++) {

            //输出每秒令牌数（或QPS增长数）
            TOKEN_NUM.compareAndSet(TOKEN_NUM.get(), (i+1) * TOKEN_DROP_SPEED.get());

            System.out.println(Thread.currentThread().getName() + " " + String.valueOf(i + 1) + " " + TOKEN_NUM);
            TimeUnit.SECONDS.sleep(1);

        }

    }
}

